package edu.sysu.cshare.control;

import java.util.List;

import org.hibernate.Session;

import edu.sysu.cshare.dao.Course;
import edu.sysu.cshare.dao.User;
import edu.sysu.cshare.util.HibernateSessionFactory;

public class ClassManager {

	User actor;

	public ClassManager(User actor) throws Exception {
		super();
		if(actor.canManagerUser()){
			
			this.actor = actor;
		}else{
			throw new Exception("权限不足,请联系管理员");
		}
	}

	public void modifyCourse(Course theold ,Course thenew){
		//TODO not sure how to handle 
	}
	public void addCourse(Course course) {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		ss.save(course);
		ss.getTransaction().commit();
	}

	public void addUser(User user) {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		ss.save(user);
		
		ss.getTransaction().commit();
	}

	public void deleteUser(User user) {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		ss.delete(user);
		
		ss.getTransaction().commit();
	}

	/**
	 * 获得班级内所有的课程
	 * @return 班级内所有的课程
	 */
	public List<Course> getCourse() {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		return ss.createQuery("from Course").list();
	}
	public static Course  getCourseById(long id){
		Session ss= HibernateSessionFactory.getSession();
		return (Course) ss.load(Course.class, id);
	}

}
